package org.zzh.service.cargo.contractProduct;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zzh.dao.common.PageBean;
import org.zzh.dao.mapper.CoContractProductMapper;
import org.zzh.dao.mapper.CoExtCproductMapper;
import org.zzh.domain.cargo.contractProduct.CoContractProduct;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.locks.Lock;

@Service
public class CoContractProductServiceImpl implements CoContractProductService {
    @Autowired
    private CoContractProductMapper coContractProductMapper;


    @Autowired
    private CoExtCproductMapper coExtCproductMapper;

    @Override
    public PageBean<CoContractProduct> list(Integer page, Integer pageSize, String contractId) {
        PageHelper.startPage(page, pageSize);
        List<CoContractProduct> list = coContractProductMapper.list(contractId);
        PageInfo<CoContractProduct> coContractProductPageInfo = new PageInfo<>(list);
        PageBean<CoContractProduct> pageBean = new PageBean<>();
        pageBean.setData(coContractProductPageInfo.getList());
        pageBean.setTotalCount((int) coContractProductPageInfo.getTotal());
        pageBean.setTotalPage(coContractProductPageInfo.getPages());
        pageBean.setPage(page);
        pageBean.setPageSize(pageSize);
        return pageBean;
    }

    @Transactional
    @Override
    public void deleteByContractIds(String[] ids) {
        coContractProductMapper.deleteByContractIds(ids);
    }

    @Transactional
    @Override
    public void delete(String id) {
        coExtCproductMapper.deleteBycontractProductId(id);
        coContractProductMapper.deleteById(id);
    }

    @Override
    public CoContractProduct findById(String id) {
        CoContractProduct coContractProduct = coContractProductMapper.findById(id);
        return coContractProduct;
    }

    @Transactional
    @Override
    public void edit(CoContractProduct contractProduct) {
        if (contractProduct.getId() != null && !contractProduct.getId().isEmpty()) {
            coContractProductMapper.update(contractProduct);
            return;
        }
        contractProduct.setId(UUID.randomUUID().toString());
        coContractProductMapper.add(contractProduct);
        return;
    }

    @Transactional
    @Override
    public void deleteById(String id) {
        coExtCproductMapper.deleteBycontractProductId(id);
        coContractProductMapper.deleteById(id);
    }

    @Override
    public void addCoContractProductList(List<CoContractProduct> list,  String contractId) {

    }

}
